package com.qyc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qyc.dto.AddOrUpdateQuarantineRegistrationDTO;
import com.qyc.enums.CodeMsgEnum;
import com.qyc.exception.SystemException;
import com.qyc.pojo.QuarantineRegistration;
import com.qyc.service.QuarantineRegistrationService;
import com.qyc.mapper.QuarantineRegistrationMapper;
import com.qyc.utils.BeanCopyUtils;
import com.qyc.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【quarantine_registration(检疫登记)】的数据库操作Service实现
* @createDate 2023-06-23 22:58:34
*/
@Service
public class QuarantineRegistrationServiceImpl extends ServiceImpl<QuarantineRegistrationMapper, QuarantineRegistration>
    implements QuarantineRegistrationService{

    //注入质检管理Mapper层对象
    @Autowired
    private QuarantineRegistrationMapper quarantineRegistrationMapper;
    /**
     * @Author qinyc
     * @Description  分页多条件查询检疫记录
     * @version: v1.0
     * @Date 23:16 2023/6/23
     **/
    @Override
    public ResponseResult queryPage(String bQualified, String grMechanism, Integer pageNum, Integer pageSize) {
        //创建条件选择器
        LambdaQueryWrapper<QuarantineRegistration> quarantineRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据检疫结果查询(精确匹配)
        quarantineRegistrationLambdaQueryWrapper.eq(StringUtils.hasText(bQualified),QuarantineRegistration::getbQualified,bQualified);
        //根据检疫机构查询(模糊匹配)
        quarantineRegistrationLambdaQueryWrapper.like(StringUtils.hasText(grMechanism),QuarantineRegistration::getGrMechanism,grMechanism);
        //开启PageHelper分页插件
        PageHelper.startPage(pageNum,pageSize,true);
        //根据条件选择器查询
        List<QuarantineRegistration> quarantineRegistrations = quarantineRegistrationMapper.selectList(quarantineRegistrationLambdaQueryWrapper);
        //封装PageInfo对象
        PageInfo objectPageInfo = new PageInfo<>(quarantineRegistrations);
        //返回统一响应
        return ResponseResult.success(objectPageInfo);
    }

    /**
     * @Author qinyc
     * @Description  新增或更新检疫记录
     * @version: v1.0
     * @Date 23:39 2023/6/23
     **/
    @Override
    public ResponseResult saveOrUpdate(AddOrUpdateQuarantineRegistrationDTO addOrUpdateQuarantineRegistrationDTO) {
        try {
            //判断是否含ID
            if (Objects.isNull(addOrUpdateQuarantineRegistrationDTO.getGrId())){
                /*
                 * ID为空 为新增操作
                 */
                //bean拷贝
                QuarantineRegistration addQuarantineRegistration = BeanCopyUtils.copyBean(addOrUpdateQuarantineRegistrationDTO, QuarantineRegistration.class);
                //执行添加操作
                quarantineRegistrationMapper.insert(addQuarantineRegistration);
                //返回统一响应
                return ResponseResult.success();
            }else {
                /*
                 * ID不为空 为修改操作
                 */
                //bean拷贝
                QuarantineRegistration updateQuarantineRegistration = BeanCopyUtils.copyBean(addOrUpdateQuarantineRegistrationDTO, QuarantineRegistration.class);
                //执行修改操作
                quarantineRegistrationMapper.updateById(updateQuarantineRegistration);
                //返回统一响应
                return ResponseResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new SystemException(CodeMsgEnum.ADD_UPDATE_QUARANTINE_REGISTRATION_ERROR);
        }
    }

    /**
     * @Author qinyc
     * @Description  根据检疫id删除检疫记录
     * @version: v1.0
     * @Date 10:17 2023/6/24
     **/
    @Override
    public ResponseResult deleteById(Integer grId) {
        if (Objects.isNull(grId)){
            //检疫ID为空
            throw new SystemException(CodeMsgEnum.QUARANTINE_REGISTRATION_ID_IS_NOT_NULL);
        }
        //只可以删除已经存在的数据
        //根据ID查询检疫记录
        QuarantineRegistration quarantineRegistration = quarantineRegistrationMapper.selectById(grId);
        if (Objects.isNull(quarantineRegistration)){
            //没有查询到指定检疫记录
            throw new SystemException(CodeMsgEnum.QUARANTINE_REGISTRATION_IS_NOT_EXIST);
        }
        //参数校验通过
        //根据ID删除指定检疫记录
        quarantineRegistrationMapper.deleteById(grId);
        //返回统一响应信息
        return ResponseResult.success();
    }

    /**
     * @Author qinyc
     * @Description  批量删除检疫记录
     * @version: v1.0
     * @Date 10:49 2023/6/24
     **/
    @Override
    public ResponseResult deleteByIdAll(List<Integer> grIds) {
        //执行批量删除
        quarantineRegistrationMapper.deleteBatchIds(grIds);
        //返回统一响应
        return ResponseResult.success();
    }
}




